﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Linq;
using WPFExcelReport.Helpers;
using System.Windows.Input;
using System.Windows.Controls;
namespace WPFExcelReport
{
    /// <summary>
    /// 专用于布卷剪布
    /// </summary>
    public partial class TWV03WB06
    {
        #region ==PACKNR===========
        public static readonly DependencyProperty PACKNRProperty = DependencyProperty.Register("PACKNR",
            typeof(string), typeof(TWV03WB06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPACKNRChanged));
        public string PACKNR
        {
            get
            {
                return (string)GetValue(PACKNRProperty);
            }
            set
            {
                SetValue(PACKNRProperty, value);
                OnPACKNRChg();
            }
        }
        private static void OnPACKNRChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWV03WB06;
            if (src != null)
            {
                src.PACKNR = (string)e.NewValue;
            }
        }
        public virtual void OnPACKNRChg()
        { }
        #endregion
        #region ==STKNR===========
        public static readonly DependencyProperty STKNRProperty = DependencyProperty.Register("STKNR",
            typeof(string), typeof(TWV03WB06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSTKNRChanged));
        public string STKNR
        {
            get
            {
                return (string)GetValue(STKNRProperty);
            }
            set
            {
                SetValue(STKNRProperty, value);
                OnSTKNRChg();
            }
        }
        private static void OnSTKNRChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWV03WB06;
            if (src != null)
            {
                src.STKNR = (string)e.NewValue;
            }
        }

        #endregion

        private double MinPack = 9999;
        #region ==FreeWBB===========
        public static readonly DependencyProperty FreeWBBProperty = DependencyProperty.Register("FreeWBB",
            typeof(double), typeof(TWV03WB06),
            new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFreeWBBChanged));
        public double FreeWBB
        {
            get
            {
                return (double)GetValue(FreeWBBProperty);
            }
            set
            {
                SetValue(FreeWBBProperty, value);
                OnFreeWBBChg(value);
            }
        }
        private static void OnFreeWBBChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWV03WB06;
            if (src != null)
            {
                src.FreeWBB = (double)e.NewValue;
            }
        }
        public void OnFreeWBBChg(double _freewbb)
        {
            if (MinPack > _freewbb)
            {
                MinPack = _freewbb;
            }
        }
        #endregion
        #region ==IsZoneWBFull===========
        public static readonly DependencyProperty IsZoneWBFullProperty = DependencyProperty.Register("IsZoneWBFull",
            typeof(bool), typeof(TWV03WB06),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsZoneWBFullChanged));
        public bool IsZoneWBFull
        {
            get
            {
                return (bool)GetValue(IsZoneWBFullProperty);
            }
            set
            {
                SetValue(IsZoneWBFullProperty, value);
                OnIsZoneWBFullChg(value);
            }
        }
        private static void OnIsZoneWBFullChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWV03WB06;
            if (src != null)
            {
                src.IsZoneWBFull = (bool)e.NewValue;
            }
        }
        public void OnIsZoneWBFullChg(bool value)
        {
        }

        #endregion
        #region ==VVKEY1===========
        public static readonly DependencyProperty VVKEY1Property = DependencyProperty.Register("VVKEY1",
            typeof(string), typeof(TWV03WB06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVVKEY1Changed));
        public string VVKEY1
        {
            get
            {
                return (string)GetValue(VVKEY1Property);
            }
            set
            {
                SetValue(VVKEY1Property, value);
            }
        }
        private static void OnVVKEY1Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWV03WB06;
            if (src != null)
            {
                src.VVKEY1 = (string)e.NewValue;

            }
        }
        #endregion       
        #region ==VVKEY2===========
        public static readonly DependencyProperty VVKEY2Property = DependencyProperty.Register("VVKEY2",
            typeof(string), typeof(TWV03WB06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVVKEY2Changed));
        public string VVKEY2
        {
            get
            {
                return (string)GetValue(VVKEY2Property);
            }
            set
            {
                SetValue(VVKEY2Property, value);
            }
        }
        private static void OnVVKEY2Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWV03WB06;
            if (src != null)
            {
                src.VVKEY2 = (string)e.NewValue;

            }
        }
        #endregion       
        #region ==VVKEY3===========
        public static readonly DependencyProperty VVKEY3Property = DependencyProperty.Register("VVKEY3",
            typeof(string), typeof(TWV03WB06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVVKEY3Changed));
        public string VVKEY3
        {
            get
            {
                return (string)GetValue(VVKEY3Property);
            }
            set
            {
                SetValue(VVKEY3Property, value);
            }
        }
        private static void OnVVKEY3Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWV03WB06;
            if (src != null)
            {
                src.VVKEY3 = (string)e.NewValue;

            }
        }
        #endregion       
        #region ==VVKEY4===========
        public static readonly DependencyProperty VVKEY4Property = DependencyProperty.Register("VVKEY4",
            typeof(string), typeof(TWV03WB06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVVKEY4Changed));
        public string VVKEY4
        {
            get
            {
                return (string)GetValue(VVKEY4Property);
            }
            set
            {
                SetValue(VVKEY4Property, value);
            }
        }
        private static void OnVVKEY4Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWV03WB06;
            if (src != null)
            {
                src.VVKEY4 = (string)e.NewValue;

            }
        }
        #endregion       
        #region ==VVKEY5===========
        public static readonly DependencyProperty VVKEY5Property = DependencyProperty.Register("VVKEY5",
            typeof(string), typeof(TWV03WB06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVVKEY5Changed));
        public string VVKEY5
        {
            get
            {
                return (string)GetValue(VVKEY5Property);
            }
            set
            {
                SetValue(VVKEY5Property, value);
            }
        }
        private static void OnVVKEY5Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWV03WB06;
            if (src != null)
            {
                src.VVKEY5 = (string)e.NewValue;

            }
        }
        #endregion       
        #region ==VVKEY6===========
        public static readonly DependencyProperty VVKEY6Property = DependencyProperty.Register("VVKEY6",
            typeof(string), typeof(TWV03WB06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVVKEY6Changed));
        public string VVKEY6
        {
            get
            {
                return (string)GetValue(VVKEY6Property);
            }
            set
            {
                SetValue(VVKEY6Property, value);
            }
        }
        private static void OnVVKEY6Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWV03WB06;
            if (src != null)
            {
                src.VVKEY6 = (string)e.NewValue;

            }
        }
        #endregion


        #region ==IsVVKEY1===========
        public static readonly DependencyProperty IsVVKEY1Property = DependencyProperty.Register("IsVVKEY1",
            typeof(bool), typeof(TWV03WB06),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsVVKEY1Changed));
        public bool IsVVKEY1
        {
            get
            {
                return (bool)GetValue(IsVVKEY1Property);
            }
            set
            {
                SetValue(IsVVKEY1Property, value);
            }
        }
        private static void OnIsVVKEY1Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWV03WB06;
            if (src != null)
            {
                src.IsVVKEY1 = (bool)e.NewValue;
            }
        }
        #endregion
        #region ==IsVVKEY2===========
        public static readonly DependencyProperty IsVVKEY2Property = DependencyProperty.Register("IsVVKEY2",
            typeof(bool), typeof(TWV03WB06),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsVVKEY2Changed));
        public bool IsVVKEY2
        {
            get
            {
                return (bool)GetValue(IsVVKEY2Property);
            }
            set
            {
                SetValue(IsVVKEY2Property, value);
            }
        }
        private static void OnIsVVKEY2Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWV03WB06;
            if (src != null)
            {
                src.IsVVKEY2 = (bool)e.NewValue;
            }
        }
        #endregion
        #region ==IsVVKEY3===========
        public static readonly DependencyProperty IsVVKEY3Property = DependencyProperty.Register("IsVVKEY3",
            typeof(bool), typeof(TWV03WB06),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsVVKEY3Changed));
        public bool IsVVKEY3
        {
            get
            {
                return (bool)GetValue(IsVVKEY3Property);
            }
            set
            {
                SetValue(IsVVKEY3Property, value);
            }
        }
        private static void OnIsVVKEY3Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWV03WB06;
            if (src != null)
            {
                src.IsVVKEY3 = (bool)e.NewValue;
            }
        }
        #endregion
        #region ==IsVVKEY4===========
        public static readonly DependencyProperty IsVVKEY4Property = DependencyProperty.Register("IsVVKEY4",
            typeof(bool), typeof(TWV03WB06),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsVVKEY4Changed));
        public bool IsVVKEY4
        {
            get
            {
                return (bool)GetValue(IsVVKEY4Property);
            }
            set
            {
                SetValue(IsVVKEY4Property, value);
            }
        }
        private static void OnIsVVKEY4Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWV03WB06;
            if (src != null)
            {
                src.IsVVKEY4 = (bool)e.NewValue;
            }
        }
        #endregion
        #region ==IsVVKEY5===========
        public static readonly DependencyProperty IsVVKEY5Property = DependencyProperty.Register("IsVVKEY5",
            typeof(bool), typeof(TWV03WB06),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsVVKEY5Changed));
        public bool IsVVKEY5
        {
            get
            {
                return (bool)GetValue(IsVVKEY5Property);
            }
            set
            {
                SetValue(IsVVKEY5Property, value);
            }
        }
        private static void OnIsVVKEY5Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWV03WB06;
            if (src != null)
            {
                src.IsVVKEY5 = (bool)e.NewValue;
            }
        }
        #endregion
        #region ==IsVVKEY6===========
        public static readonly DependencyProperty IsVVKEY6Property = DependencyProperty.Register("IsVVKEY6",
            typeof(bool), typeof(TWV03WB06),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsVVKEY6Changed));
        public bool IsVVKEY6
        {
            get
            {
                return (bool)GetValue(IsVVKEY6Property);
            }
            set
            {
                SetValue(IsVVKEY6Property, value);
            }
        }
        private static void OnIsVVKEY6Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWV03WB06;
            if (src != null)
            {
                src.IsVVKEY6 = (bool)e.NewValue;
            }
        }
        #endregion
        /// <summary>
        /// 1.获取箱子号，传递给界面
        /// 2.左键点击布卷，更改箱子号
        /// 3.有个键取消
        /// 3.确认后，过滤掉
        /// </summary>
        public TWV03WB06()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVVTLIEF();
            this.DataContext = MV;
            InitializeComponent();
        }
        public override void OnEXAUFChg(string vlaue)
        {
            IsVVKEY1 = false;
            IsVVKEY2 = false;
            IsVVKEY3 = false;
            IsVVKEY4 = false;
            IsVVKEY5 = false;
            IsVVKEY6 = false;
            VVKEY1 = "";
            VVKEY2 = "";
            VVKEY3 = "";
            VVKEY4 = "";
            VVKEY5 = "";
            VVKEY6 = "";
            TBMain.Clear();
            LIEFSCH = "";
            if (EXAUF.Length < 20)
                return;
            LIEFSCH = EXAUF.Substring(12, 8);
            dwData = TBMain.NewRow();
            dwData["VVTMANDANT"] = app.Mandant;
            dwData["VVTLIEFSCH"] = LIEFSCH;
            SResult = TMV.QVKPBYVVT(dwData);
            MergeData(SResult, false);
            int idx = 0;
            foreach (DataRow row in TBMain.Rows)
            {
                idx++;
                string key = row["VVTAUFTYP"].ToString() + row["VVTAUFKRS"].ToString() + row["VVTAUFLFD"].ToString() + row["VVTAUFPOS"].ToString() + row["VVTLIEFSCH"].ToString();
                switch (idx)
                {
                    case 1:
                        IsVVKEY1 = true;
                        VVKEY1 = key;
                        break;
                    case 2:
                        IsVVKEY2 = true;
                        VVKEY2 = key;
                        break;
                    case 3:
                        IsVVKEY3 = true;
                        VVKEY3 = key;
                        break;
                    case 4:
                        IsVVKEY4 = true;
                        VVKEY4 = key;
                        break;
                    case 5:
                        IsVVKEY5 = true;
                        VVKEY5 = key;
                        break;
                    case 6:
                        IsVVKEY6 = true;
                        VVKEY6 = key;
                        break;
                }
            }
            base.OnEXAUFChg(vlaue);
        }
        double packnr = 0;
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                dwData = TBMain.NewRow();
                dwData["VVTMANDANT"] = app.Mandant;
                dwData["VVTLIEFSCH"] = LIEFSCH;
                SResult = TMV.QGETPACKNR(dwData);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    packnr = double.Parse(row["WBBPACKNR"].ToString());
                }
                packnr++;
                PACKNR = Helper.Right("0000" + packnr.ToString(), 4);
                base.NewCommand(sender, e);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        /// <summary>
        /// 布卷清单处于自动更改包装号状态
        /// 获取最小的包装号
        /// 自动增长。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void AutoPackCommand(object sender, RoutedEventArgs e)
        {
            IsAuto = false;
            IsAuto = true;
            dwData = TBMain.NewRow();
            dwData["VVTMANDANT"] = app.Mandant;
            dwData["VVTLIEFSCH"] = LIEFSCH;
            SResult = TMV.QGETPACKNR(dwData);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                packnr = double.Parse(row["WBBPACKNR"].ToString());
            }
            while (true)
            {
                MinPack--;
                packnr++;
                PACKNR = Helper.Right("0000" + packnr.ToString(), 4);
                if (MinPack < 0)
                    break;
            }
            IsAuto = false;
        }
        public virtual void OnSTKNRChg()
        {

            if (STKNR == "")
                return;
            PM["STKNR"] = STKNR;
            PM["LIEFSCH"] = LIEFSCH;
            PACKNR = TMV.VWBB.CHKVVPACK(PM);
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            IsSaved = !IsSaved;
            IsSaved = !IsSaved;
            //base.SaveCommand(sender, e);
        }

        public override void Query_KeyUp(object sender, KeyEventArgs e)
        {
            base.Query_KeyUp(sender, e);
        }
    }
}
